﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using CommomFunctions;
using BinaryTree;
using System.Windows.Media.Animation;

namespace Árvore_Binária_de_Pesquisa
{
    public class Labels : CommomFunctions.Labels
    {
        /// <summary>
        /// numNodes -> maximum number of elements in the tree. Here they will represent the nodes of the tree.
        /// ordedLabelsContent -> contains the characters in such an order that if they are inserted in the
        /// tree from "H" to "O" it will generate a complete 4 levels binary tree
        /// </summary>
        private int numNodes = 15;
        private Array ordedLabelsContent = new string[] { "H", "D", "L", "B", "F", "J", "N", "A", "C", "E", "G", "I", "K", "M", "O" };
        
        public void createLabels(Grid gd)
        {
            createElements(gd);           
            gridSizeChanged();
        }

        /// <summary>
        /// Creates the Labels that will be the nodes of the tree
        /// </summary>
        /// <param name="gd">The grid to which the labels will be added</param>         
        private void createElements(Grid gd)
        {
            for (int i = 0; i < numNodes; i++)
            {
                Label tempLabel = new Label();
                gd.Children.Add(tempLabel);
                labels.Add(tempLabel);
            }

            foreach (Label lbl in labels)
            {
                lbl.Visibility = Visibility.Hidden;
                lbl.Content = ordedLabelsContent.GetValue(labels.IndexOf(lbl)).ToString();
                lbl.VerticalAlignment = VerticalAlignment.Bottom;
                lbl.HorizontalAlignment = HorizontalAlignment.Center;
                lbl.HorizontalContentAlignment = HorizontalAlignment.Center;
                lbl.VerticalContentAlignment = VerticalAlignment.Center;
                lbl.FontSize = 19;
                lbl.FontFamily = new FontFamily("Lucida");
                lbl.Foreground = Brushes.SeaShell;
                lbl.FontWeight = FontWeights.Bold;
                lbl.MinWidth = 70;
                lbl.MaxWidth = 110;
                lbl.MinHeight = 38;
                lbl.Background = Brushes.ForestGreen;
                lbl.BorderBrush = Brushes.Transparent;
                lbl.BorderThickness = new Thickness(0, 4, 0, 4);
                Grid.SetColumn(lbl, 0);
                Grid.SetRow(lbl, 1);
                Grid.SetRowSpan(lbl, 6);
                Grid.SetColumnSpan(lbl, 9);
            }
        }  
               
        /// <summary>
        /// Fills the Labels with orded content and insert the node to the tree
        /// </summary>
        /// <param name="bt"></param>
        public void insertSortedLabels(TBinarySTree bt)
        {
            for (int i = 0; i < numNodes; i++)
            {
                ((Label)labels[i]).Content = ordedLabelsContent.GetValue(i).ToString();
                bt.insert(((Label)labels[i]).Content.ToString());
            }
        }

        /// <summary>
        /// Sets the labels' margins when the grid size changes
        /// </summary>
        public void gridSizeChanged()
        {
            double gw = 994 / 52.0;
            double gh = 674 / 36.0;

            ((Label)(labels[0])).Margin = new Thickness(gw * 12, gh, gw * 12, gh * 21);
            ((Label)(labels[1])).Margin = new Thickness(gw * 10, gh * 7, gw * 34, gh * 15);
            ((Label)(labels[2])).Margin = new Thickness(gw * 34, gh * 7, gw * 10, gh * 15);

            ((Label)(labels[3])).Margin = new Thickness(gw * 4, gh * 13, gw * 40, gh * 9);
            ((Label)(labels[4])).Margin = new Thickness(gw * 16, gh * 13, gw * 28, gh * 9);
            ((Label)(labels[5])).Margin = new Thickness(gw * 28, gh * 13, gw * 16, gh * 9);
            ((Label)(labels[6])).Margin = new Thickness(gw * 40, gh * 13, gw * 4, gh * 9);

            ((Label)(labels[7])).Margin = new Thickness(gw * 1, gh * 19, gw * 43, gh * 3);
            ((Label)(labels[8])).Margin = new Thickness(gw * 7, gh * 19, gw * 37, gh * 3);
            ((Label)(labels[9])).Margin = new Thickness(gw * 13, gh * 19, gw * 31, gh * 3);
            ((Label)(labels[10])).Margin = new Thickness(gw * 19, gh * 19, gw * 25, gh * 3);
            ((Label)(labels[11])).Margin = new Thickness(gw * 25, gh * 19, gw * 19, gh * 3);
            ((Label)(labels[12])).Margin = new Thickness(gw * 31, gh * 19, gw * 13, gh * 3);
            ((Label)(labels[13])).Margin = new Thickness(gw * 37, gh * 19, gw * 7, gh * 3);
            ((Label)(labels[14])).Margin = new Thickness(gw * 43, gh * 19, gw * 1, gh * 3);
        }
    }
}
